<%@page import="com.bean.HistoryBean"%>
<%@page import="com.bean.RecordBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	@SuppressWarnings("unchecked")
	ArrayList<RecordBean> list = (ArrayList<RecordBean>)request.getAttribute("list"); 
	HistoryBean hisBean = (HistoryBean)request.getAttribute("message");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'statistic.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type = "text/css" href="CSS/history.css">
	<script type="text/javascript" src="JS/calendar.js"></script>
	
	<script type="text/javascript">
	function check(form)
	{
			
		var key = form.key.value;
		var value = form.value.value;
		if (key == "barcode") {
			if (value == "")
				return false;
			if (value.length > 45) {
				return false;
			}
			if (!/^\d+$/.test(value)) {
				alert('barcode is illegal,number is ok');
				return false;
			}
		}
		if (key == "userid") {
			if (value == "")
				return false;
			if (!/^\d+$/.test(value)) {
				alert('userID is illegal');
				return false;
			}
			if (value.length != 8) {
				alert('userID length is illegal,8 bits');
				return false;
			}
		}
		if (form.flag.checked) {
			var sdate = form.sdate.value;
			var edate = form.edate.value;
			if (sdate == "") {
				alert("please enter the start date!");
				return false;
			}
			if (edate == "") {
				alert("please enter the end date!");
				return false;
			}
			var reg = /^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/;
			if (!reg.test(sdate)) {
				alert("the start date format is illegal !");
				return false;
			}
			if (!reg.test(edate)) {
				alert("the end date format is illegal !");
				return false;
			}
	
		}
		form.submit();
}
	</script>
</head>

<body >
<div  style="width: 95%;" class="title1">
	<h1>History Records</h1>
    <form action="HistoryServlet" name = "form1" method="post" style="border: 0px;margin: 0;padding: 2">
	<table align="center" height="29">
	<tr>
	  <td >
	  <%
	  		String key = "barcode";
	  		String value = "";
	  	    boolean check = false;
	  	    String startDate = "";
	  	    String endDate = "";
	  		if(null != hisBean)
	  		{
	  		 key = hisBean.getKey();
	  		 value = hisBean.getValue();
	  		 check = hisBean.isDate();
	  		 if(check)
	  		 {
	  			 startDate = hisBean.getsDate().toString();
	  			 endDate = hisBean.geteDate().toString();
	  		 }
	  		}
	  %>
		<select name="key">
           <option value="barcode"<%if(key.equals("barcode")){%> selected <%}%>>Barcode</option>
           <option value="userid" <%if(key.equals("userid")){%> selected <%}%>>User ID</option>
        </select>

        <input name="value" value="<%=value %>" type="text"  size="37" maxlength="45">

		<input name="flag" type="checkbox" value="date" <%if(check){%>checked<%} %>>
	    Time:From
	   <input name="sdate" type="text" value="<%=startDate%>" size="27"  onclick="new Calendar(null, null, 1,null,'yyyy-MM-dd').show(this);">
	   To
	    <input name="edate" type="text" value="<%=endDate%>" size="27" onclick="new Calendar(null, null, 1,null,'yyyy-MM-dd').show(this);">
		  <td width="10%"><input type = "button" value = "submit" onclick="return check(form1);">
		  </td>
   </tr>
  </table>
</form>
</div>
<%if((list==null) || (list.isEmpty()))
{%>
     <table width="95%" height="10"  border="0" cellpadding="0" cellspacing="0">
       <tr>
         <td height="10" align="center" valign="top"><font color="red" size="3px">No records</font></td>
       </tr>
     </table>
<%} %>
<table width="95%" id="mytab"  border="1" class="t1">

<%
	if("userid".equals(key))
	{
%>
  <thead>
  <tr>
    <th width="4%">num</th>
    <th width="10%">UserID</th>
    <th width="10%">FirstName</th>
    <th width="10%">LastName</th>
    <th width="13%">E-mail</th>
    <th width="11%">Bar-code</th>
    <th width="13%">Instrument Name</th>
    <th width="10%">BorrowDate</th>
    <th width="10%">BackDate</th>
   </tr>
  </thead>
<%} else {%>
	<thead>
  <tr>
    <th width="4%">num</th>
    <th width="11%">Bar-code</th>
    <th width="13%">Instrument Name</th>
    <th width="10%">UserID</th>
    <th width="10%">FirstName</th>
    <th width="10%">LastName</th>
    <th width="13%">E-mail</th>
    <th width="10%">BorrowDate</th>
    <th width="10%">BackDate</th>
   </tr>
  </thead>
<%} %>
 <tbody style="overflow: scroll; height: 5px">
<%
	String insName = "";
	String barcode = "";
	String userID = "";
	String FirstName = "";
	String LastName = "";
	String email = "";
	String BorrowDate = "";
	String BackDate = "";
	int num = 0;
	if(null != list && list.size()!=0)
	{
		Iterator<RecordBean> iter = list.iterator();
		while(iter.hasNext())
		{
			RecordBean his = iter.next();
			insName = his.getInsName();
			barcode = his.getBarcode();
			userID = his.getUserID();
			FirstName = his.getFirstname();
			LastName = his.getLastname();
			email = his.getEmail();
			BorrowDate = his.getBorrowDate().toString();
			BackDate = his.getBackDate().toString();
			num++;
			if("userid".equals(key))
			{
%>
  			<tr <%if(num%2!=0){ %>class="a1"<%} %>>
  			<td><%=num %></td>
   			<td><%=userID%></td>
   			<td><%=FirstName %></td>
   			<td><%=LastName%></td>
   			<td><%=email%></td>
   			<td><%=barcode %></td>
  			<td><%=insName %></td>
   			<td><%=BorrowDate %></td>
   			<td><%=BackDate%></td>
  			</tr>
<%	 		}else{%>
			<tr<%if(num%2!=0){ %>class="a1"<%} %>>
			<td><%=num %></td>
			<td><%=barcode %></td>
			<td><%=insName %></td>
			<td><%=userID%></td>
			<td><%=FirstName %></td>
			<td><%=LastName%></td>
			<td><%=email%></td>
			<td><%=BorrowDate %></td>
			<td><%=BackDate%></td>
			</tr>
			<%} %>	
<%
		}
	}else{
%>
	
			<tr>
			<td><%=num %></td>
			<td><%=insName %></td>
			<td><%=barcode %></td>
			<td><%=userID%></td>
			<td><%=FirstName %></td>
			<td><%=LastName%></td>
			<td><%=email%></td>
			<td><%=BorrowDate %></td>
			<td><%=BackDate%></td>
			</tr>



 <%} %>	
 </tbody>
</table>

</body>
</html>
